package com.cr.mrapp.Controller;

import com.cr.mrapp.entity.Staff;
import com.cr.mrapp.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/staff")
public class StaffController {

    @Autowired
    private StaffService staffService;

    @RequestMapping("/addStaff.html")
    public String toAddStaff(){
        return "addStaff";
    }

    @RequestMapping("/staffList.html")
    public String tostaffList() { return "staffList"; }

    @RequestMapping("/add.do")
    @ResponseBody
    public Object addStaff(@RequestBody Staff body){
        int row = staffService.addStaff(body);
        if(row>0){
            return "ok";
        }else{
            return "fail";
        }
    }

    @RequestMapping("/delete.do")
    @ResponseBody
    public Object deleteById(int id){
        Map<String,Object> responseData = new HashMap<>();
        int row = staffService.deleteById(id);
        if(row>0){
            responseData.put("code", 200);
            responseData.put("msg","删除成功");
            responseData.put("data",row);
        }else {
            responseData.put("code", 500);
            responseData.put("msg","删除失败");
            responseData.put("data",0);
        }
        return responseData;
    }

    @RequestMapping("/staffList.do")
    @ResponseBody
    public Object findByPage(int page, int limit, String staffName){

        List<Object> list = staffService.findByPage(page, limit, null);
        int count = staffService.findCount();

        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","查询成功");
        map.put("count",count);
        map.put("data",list);
        return map;
    }
}
